package com.hut.oa.model.dao.user;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.hut.oa.model.bean.Dept;
import com.hut.oa.model.bean.User;


public interface UserDao extends PagingAndSortingRepository<User, Long>{
	Page<User> findAll(Pageable pageable);
	
	User findByNameAndPassword(String name,String password);
	
	User findByName(String name);
	
	User findById(Long id);
	
	List<User> findByIdNot(Long id);
	
	Page<User> findByIdNot(Pageable pageable,Long id);
	
	Page<User> findByDept(Dept dept,Pageable pageable);
	
	@Query("from User u where u.realName like %:key% or u.tel like %:key% ")
	Page<User> findByRealNameLikeOrTelLike(@Param("key")String key,Pageable pageable);
	
	@Query("from User u where u.realName like %:key%")
	Page<User> findByRealNameLikeLike(@Param("key")String key,Pageable pageable);

	@Query("from User u where (u.realName like %:key% or u.tel like %:key%) and u.dept=:dept ")
	Page<User> findByRealNameOrTelAndDept(@Param("key")String key,@Param("dept")Dept dept,Pageable pageable);
	
	@Query("from User u where u.realName like %:key%  and u.dept=:dept ")
	Page<User> findByRealNameAndDept(@Param("key")String key,@Param("dept")Dept dept,Pageable pageable);
	
	@Query("from User u where u.realName like %:key% or u.tel like %:key% ")
	List<User> findByRealNameOrTel(@Param("key")String key);
	
	@Query("from User u where u.realName like %:name% and id!=:id")
	List<User> findbyrealnameandnotid(@Param("name")String name,@Param("id")Long id);
	
	@Query("update User u set u.name=:name,u.password=:pwd where u.id=:id")
	@Modifying
	int updateAccount(@Param("name")String name,@Param("pwd")String pwd,@Param("id")Long id);
	
	@Query("update User u set u.name=:name,u.password=:pwd,u.skin=:skin where u.id=:id")
	@Modifying
	int updateSystem(@Param("name")String name,@Param("pwd")String pwd,@Param("skin")String skin,@Param("id")Long id);

	@Query("update User u set u.tel=:tel,u.email=:email,u.address=:address,u.bank=:bank,u.bir=:bir where u.id=:id")
	@Modifying
	int updateBase(@Param("tel")String tel,@Param("email")String email,@Param("address")String address,@Param("bank")String bank,@Param("bir")Date bir,@Param("id")Long id);

}
